package com.zl.payment.controller;

import com.zl.payment.entity.InstallmentBill;
import com.zl.payment.entity.InstallmentRate;
import com.zl.payment.entity.InstallmentRecord;
import com.zl.payment.entity.Repayment;
import com.zl.payment.mapper.InstallmentBillMapper;
import com.zl.payment.mapper.InstallmentRateMapper;
import com.zl.payment.mapper.InstallmentRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

/*
@RestController
@RequestMapping("/payment-service/installment")
public class InstallmentController {
    @Autowired
    private InstallmentBillMapper installmentBillMapper; //分期账单
    @Autowired
    private InstallmentRateMapper installmentRateMapper;//分期利息
    @Autowired
    private InstallmentRecordMapper installmentRecordMapper;//分期记录

    */
/**
     * 可分期额度
     *
     * @param totalMoney
     * @param model
     * @return
     *//*

    */
/*@RequestMapping("canInstallment")
    public String canInstallment(BigDecimal totalMoney, Model model) {
        BigDecimal mayInstallment = null;//可分期额度
        if (totalMoney.compareTo(new BigDecimal(1000)) > -1) {
            mayInstallment = totalMoney.multiply(new BigDecimal("0.9"));
            model.addAttribute("mayInstallment", mayInstallment);
        } else {
            model.addAttribute("mayInstallment", mayInstallment);
        }
        return "可分期额度： " + mayInstallment;
    }*//*


    */
/**
     * 每月应还
     *
     * @param numStage         分期期数
     * @param InstallmentMoney 分期金额
     * @param model
     * @return
     *//*

    @RequestMapping("everyMonth")
    public String everyMonth(Integer numStage, BigDecimal InstallmentMoney, Model model) {
        Map<String, Object> map = new HashMap<>();
        map.put("BILL_NUMBERS", numStage);
        List<InstallmentRate> installmentRates = installmentRateMapper.selectByMap(map);
        BigDecimal billRate = null;//利息
        for (int i = 0; i <= installmentRates.size(); i++) {
            billRate = installmentRates.get(0).getBillRate().divide(new BigDecimal("100"));
        }
       */
/* QueryWrapper<InstallmentRate> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("BILL_NUMBERS",numStage);
        List<InstallmentRate> installmentRates = installmentRateMapper.selectList(objectQueryWrapper);
        BigDecimal billRate = null;
        for (int i = 0; i <= installmentRates.size(); i++) {
            billRate=installmentRates.get(0).getBillRate();
        }*//*

        BigDecimal commission = (InstallmentMoney.multiply(new BigDecimal(billRate.toString()))).divide(new BigDecimal(numStage));//每期手续费
        BigDecimal everyMouthYet = (InstallmentMoney.multiply(new BigDecimal(String.valueOf(billRate))).add(InstallmentMoney)).divide(new BigDecimal(numStage));//每月应还
        model.addAttribute("billRate", billRate);
        model.addAttribute("commission", commission);
        model.addAttribute("everyMouthYet", everyMouthYet);
        return "利息: " + (billRate.multiply(new BigDecimal("100")))+"%" + "    每期手续费: " + commission + "   每月应还: " + everyMouthYet;
    }

    @RequestMapping("repay")
    public String repayment(Repayment repay) {
        //1、分期记录
        InstallmentRecord record = new InstallmentRecord();
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        String uuid1=uuid.substring(0, 12);
        record.setRecordId(uuid1);//分期记录编号
        record.setBillId(repay.getConsumBillId());//消费账单编号
        record.setBillAmount(repay.getBillAmount());//消费账单总金额
        record.setBillInstallmentAmount(repay.getInstallmentMoney());//分期总金额
        record.setBillInsatllmentNumber(repay.getNumStage());//分期总期数
        record.setCreditCardId(repay.getCreditCardId());//信用卡号
        record.setBillDate(new Date());//分期申请时间

        Calendar calendar = Calendar.getInstance();
        //获得当前时间的月份bai，月份从0开始所以结果要加1
        String date = calendar.get(Calendar.YEAR) + "/" + (calendar.get(Calendar.MONTH) + 1);
        record.setBillStartMonth(date);//分期账单开始月份

        int num1 = installmentRecordMapper.insert(record);

        //2、分期账单表
        InstallmentBill bill = new InstallmentBill();
        String uuid2 = UUID.randomUUID().toString().replaceAll("-", "").substring(0, 12);
        bill.setBillId(uuid2);//分期记录编号
        bill.setTotalPeriods(repay.getNumStage());//分期总期数
        bill.setCreditCardId(repay.getCreditCardId());//信用卡账号
        bill.setBillAmount(repay.getInstallmentMoney());//分期总额
        bill.setBillRatenum(repay.getCommission());//手续费
        bill.setRecordId(uuid1);//分期记录ID
        bill.setCurrentPeriod(1);//当前期数
        bill.setBillStatus(0);//账单状态，默认为未出账
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        String format=sdf.format(new Date());
        bill.setBillingMonth(format);//账单时间

        int num2 = installmentBillMapper.insert(bill);


        return "分期记录表成功添加记录数: " + num1+"    分期账单表成功添加记录数: "+num2;
    }


}
*/
